package cn.jbolt.xd;

import cn.jbolt.common.model.xd.XdBillXingdongZsyh0608;
import cn.jbolt.core.base.config.JBoltConfig;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.druid.DruidPlugin;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Main {


    public static void main(String[] args) {

        XdDBUtils.contect();
         try {
             read();
         }catch (Exception e){
             e.printStackTrace();
         }
    }

    public static void read() throws Exception{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        // 读取磁盘上已经存在的Excel文件 传入参数需是对应Excel文件路径的输入流对象
        String path = "/Users/weizixiong/星冻/星冻流水/表格/未命名文件夹/";
        String file = "对账单，海南星冻供应链科技有限公司，755943680610608，人民币，2025年04月.xlsx";
        InputStream in = new FileInputStream(new File(path+file));

        XSSFWorkbook excel = new XSSFWorkbook(in);
        for (int j = 0; j < excel.getNumberOfSheets(); j++) {
            System.out.println("====第" +j+  "页====");
            // 读取Excel文件中的第一个Sheet页
            XSSFSheet sheet = excel.getSheetAt(j);

            // 获取sheet中最后一行的行号
            int lastRowNum = sheet.getLastRowNum();

            for (int i = 8; i <= lastRowNum ; i++) {
                // 获得某一行
                XSSFRow row = sheet.getRow(i);
                if (row == null){
                    continue;
                }

                row.getCell(0).setCellType(CellType.STRING);
                row.getCell(1).setCellType(CellType.STRING);
                row.getCell(2).setCellType(CellType.STRING);
                if (row.getCell(3) !=null){
                    row.getCell(3).setCellType(CellType.STRING);
                }

                row.getCell(4).setCellType(CellType.STRING);
                row.getCell(5).setCellType(CellType.STRING);
                row.getCell(6).setCellType(CellType.STRING);

                if (row.getCell(0).getStringCellValue().trim().isEmpty()){
                    break;
                }

                String date = row.getCell(0).getStringCellValue();
                String type = row.getCell(1).getStringCellValue();
                String num = row.getCell(2).getStringCellValue();

                String desc = "";
                if (row.getCell(3) !=null){
                    desc = row.getCell(3).getStringCellValue();
                }

                String amount = row.getCell(4).getStringCellValue().replace(",","").trim();
                String balance = row.getCell(5).getStringCellValue().replace(",","").trim();;
                String accnount = row.getCell(6).getStringCellValue();
//                System.out.println(i+"=="+amount);

                XdBillXingdongZsyh0608 model = new XdBillXingdongZsyh0608();
                model.setTradeTime(sdf.parse(date));
                model.setTradeType(type);
                model.setSerialNumber(num);
                model.setTradeRemark(desc);
                model.setTradeAmount(new BigDecimal(amount));
                model.setBalance(new BigDecimal(balance));
                model.setReciprocalAccountName(accnount);
                model.setCreateTime(new Date());
                model.save();
                System.out.println(i+"==="+model);

            }
        }



        // 关闭资源
        in.close();
        excel.close();
    }


}